
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>6.2 开发uts插件 · uniapp/uniapp-x for Harmony教程</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="坚果派">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-mind-maps/mindmaps.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-ace/ace.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-lou/lou.css">
                
            
        

    

    
        
        <link rel="stylesheet" href="../styles/website.css">
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3.新建鸿蒙插件.html" />
    
    
    <link rel="prev" href="1.UTS开发鸿蒙插件.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"repo":"ITmxs/gitbook","types":["star","watch","fork"],"size":"small"};
    </script>

    </head>
    <body>
         
<div class="donate-modal modal">
  <div class="modal-header">
    <div>
      <span>
        <img
          id="modal-avatar"
          class="modal-avatar"
          src="http://xkapp-uat.oss-cn-hangzhou.aliyuncs.com/2e7a3f70-80ab-4b50-93ec-a04dfeef949b/avatar-100.png"
        />
      </span>
      <span id="nickname"> breeze </span>
      <p class="modal-close close" onclick="closeDonateModal()">×</p>
    </div>
    <div class="message">
      <div class="tip">
        <i></i>
        <span>随意打赏，但不要超过一顿早餐钱 💕</span>
      </div>
    </div>
  </div>
  <div class="modal-content">
    <div class="modal-pay-way">
      <div id="wxpay" onclick="showDonateImg(0)">
        <input
          type="radio"
          name="pay-way"
          value="wxpay"
          class="zs-type"
          checked="checked"
        />
        <span>
          <img
            id="wxpay-logo"
            src=""
          />
        </span>
      </div>
      <div id="alipay" onclick="showDonateImg(1)">
        <input type="radio" name="pay-way" value="alipay" class="zs-type" />
        <span>
          <img
            id="alipay-logo"
            src=""
          />
        </span>
      </div>
    </div>
    <div class="modal-pay-qrcode">
      <img id="wxpay-code" src="https://luckly007.oss-cn-beijing.aliyuncs.com/img/image-20210922202901895.png" />
      <img
        id="alipay-code"
        src="https://luckly007.oss-cn-beijing.aliyuncs.com/img/image-20210922202843746.png"
        hidden
      />
    </div>
  </div>
</div>
<div class="mask"></div>


<div class="book">
  <div class="header-inner">
    <!-- LOGO -->
    <div class="logo"></div>
    <span class="title"></span>

    <!-- Search -->
    
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>


    <!-- Nav -->
    <ul class="header-nav">
      <li>
        <a href="https://www.nutpi.net/" target="_blank">坚果派官网</a>
      </li><li>
        <a href="https://www.arkui.club/" target="_blank">ArkUI实战</a>
      </li><li>
        <a href="https://space.bilibili.com/480883651" target="_blank">元服务开发实践</a>
      </li><li>
        <a href="https://gitcode.com/nutpi" target="_blank">OpenHarmony应用开发</a>
      </li>
    </ul>
  </div>

  <div class="book-summary">
    <div class="book-summary-title">文档目录</div>
     
    <nav role="navigation">


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    1.前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter2/1.为什么.html">
            
                <a href="../chapter2/1.为什么.html">
            
                    
                    2.为什么要这个专栏？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    3.uniapp开发鸿蒙
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter3/14.背景.md">
            
                <span>
            
                    
                    3.1背景
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter3/15.优势.html">
            
                <a href="../chapter3/15.优势.html">
            
                    
                    3.2优势
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter3/1.uniapp开发鸿蒙应用.html">
            
                <a href="../chapter3/1.uniapp开发鸿蒙应用.html">
            
                    
                    3.3uniapp开发鸿蒙应用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter3/3.uniapp之uts语言.html">
            
                <a href="../chapter3/3.uniapp之uts语言.html">
            
                    
                    3.4uniapp之uts语言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter3/4.uniapp for Harmony.html">
            
                <a href="../chapter3/4.uniapp for Harmony.html">
            
                    
                    3.5uniapp for Harmony
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../chapter3/5.uniapp for Harmony FAQ.html">
            
                <a href="../chapter3/5.uniapp for Harmony FAQ.html">
            
                    
                    3.6uniapp for Harmony FAQ
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../chapter3/2.uniapp2.html">
            
                <a href="../chapter3/2.uniapp2.html">
            
                    
                    3.7uniapp2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../chapter3/6.uniappx支持鸿蒙.html">
            
                <a href="../chapter3/6.uniappx支持鸿蒙.html">
            
                    
                    3.8uniappx支持鸿蒙
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../chapter3/7.uniapp-x支持鸿蒙.html">
            
                <a href="../chapter3/7.uniapp-x支持鸿蒙.html">
            
                    
                    3.9uniapp-x支持鸿蒙
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../chapter3/8.目前支持鸿蒙手机.html">
            
                <a href="../chapter3/8.目前支持鸿蒙手机.html">
            
                    
                    3.10目前支持鸿蒙手机
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../chapter3/9.uniapp-x项目开发鸿蒙支持调试啦.html">
            
                <a href="../chapter3/9.uniapp-x项目开发鸿蒙支持调试啦.html">
            
                    
                    3.11uniapp-x项目开发鸿蒙支持调试啦
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../chapter3/10.uniapp和flutter你会选哪个来开发鸿蒙.html">
            
                <a href="../chapter3/10.uniapp和flutter你会选哪个来开发鸿蒙.html">
            
                    
                    3.12uniapp和flutter你会选哪个来开发鸿蒙
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../chapter3/11.Flutter与uni-app的深度对比：鸿蒙开发的最佳选择竟是原生开发.md.md">
            
                <span>
            
                    
                    3.13Flutter与uni-app的深度对比：鸿蒙开发的最佳选择竟是原生开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.14" data-path="../chapter3/12.uniapp版本更新.html">
            
                <a href="../chapter3/12.uniapp版本更新.html">
            
                    
                    3.14uniapp版本更新
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.15" data-path="../chapter3/13.HBuilderX 4.62 开始，uni-app 支持嵌入鸿蒙原生组件.html">
            
                <a href="../chapter3/13.HBuilderX 4.62 开始，uni-app 支持嵌入鸿蒙原生组件.html">
            
                    
                    3.15HBuilderX 4.62开始，uni-app支持嵌入鸿蒙原生组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.16" data-path="../chapter3/16.uni-app x 已经完成鸿蒙等主流平台全覆盖.html">
            
                <a href="../chapter3/16.uni-app x 已经完成鸿蒙等主流平台全覆盖.html">
            
                    
                    3.16uni-app x 已经完成鸿蒙等主流平台全覆盖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.17" data-path="../chapter3/17.uniapp如何进行条件编译到鸿蒙.html">
            
                <a href="../chapter3/17.uniapp如何进行条件编译到鸿蒙.html">
            
                    
                    3.17uniapp如何进行条件编译到鸿蒙
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.18" data-path="../chapter3/18.鸿蒙Next案例分析.html">
            
                <a href="../chapter3/18.鸿蒙Next案例分析.html">
            
                    
                    3.18鸿蒙Next案例分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.19" data-path="../chapter3/19.UTS开发鸿蒙插件.md">
            
                <span>
            
                    
                    3.19UTS开发鸿蒙插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.20" data-path="../chapter3/20.热重载.html">
            
                <a href="../chapter3/20.热重载.html">
            
                    
                    3.20热重载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.21" data-path="../chapter3/21.调用鸿蒙原生组件.html">
            
                <a href="../chapter3/21.调用鸿蒙原生组件.html">
            
                    
                    3.21调用鸿蒙原生组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.22" data-path="../chapter3/22.从开发者视角看 uniapp 与 Flutter 的差异.html">
            
                <a href="../chapter3/22.从开发者视角看 uniapp 与 Flutter 的差异.html">
            
                    
                    3.22从开发者视角看 uniapp 与 Flutter 的差异
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.23" data-path="../chapter3/23.uniappforHarmony学习路线.html">
            
                <a href="../chapter3/23.uniappforHarmony学习路线.html">
            
                    
                    3.23uniappforHarmony学习路线
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    4.随机笑话-实战案例
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter4/1.uni-app_x跨平台实战入门：从0到1开发HarmonyOS_5鸿蒙原生应用.html">
            
                <a href="../chapter4/1.uni-app_x跨平台实战入门：从0到1开发HarmonyOS_5鸿蒙原生应用.html">
            
                    
                    4.1 uni-app x 跨平台实战入门：从 0 到 1 开发 HarmonyOS 5 鸿蒙原生应用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    5.二十四节气
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" >
            
                <span>
            
                    
                    5.1二十四节气实战案例
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    6.UTS插件开发
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="1.UTS开发鸿蒙插件.html">
            
                <a href="1.UTS开发鸿蒙插件.html">
            
                    
                    6.1 UTS开发鸿蒙插件
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6.2" data-path="2.开发uts插件.html">
            
                <a href="2.开发uts插件.html">
            
                    
                    6.2 开发uts插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="3.新建鸿蒙插件.html">
            
                <a href="3.新建鸿蒙插件.html">
            
                    
                    6.3 新建鸿蒙插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="4.鸿蒙使用插件.md">
            
                <span>
            
                    
                    6.4 鸿蒙使用插件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    7.鸿蒙插件实战
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter7/1.开发鸿蒙插件.html">
            
                <a href="../chapter7/1.开发鸿蒙插件.html">
            
                    
                    7.1 开发鸿蒙插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter7/2.uniappx插件nutpi-idcard 开发与使用指南（适配鸿蒙）.html">
            
                <a href="../chapter7/2.uniappx插件nutpi-idcard 开发与使用指南（适配鸿蒙）.html">
            
                    
                    7.2 uniappx插件nutpi-idcard开发与使用指南
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter7/3.chinese-number-format插件.html">
            
                <a href="../chapter7/3.chinese-number-format插件.html">
            
                    
                    7.3 chinese-number-format插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter7/4.calendar插件.html">
            
                <a href="../chapter7/4.calendar插件.html">
            
                    
                    7.4 calendar插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter7/5.HBuilderX 中适配鸿蒙插件的安装使用指南.html">
            
                <a href="../chapter7/5.HBuilderX 中适配鸿蒙插件的安装使用指南.html">
            
                    
                    7.5 HBuilderX中适配鸿蒙插件的安装使用指南
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../chapter7/OpenHarmony5.0跟华为HarmonyOS之间的应用生态兼容情况.html">
            
                <a href="../chapter7/OpenHarmony5.0跟华为HarmonyOS之间的应用生态兼容情况.html">
            
                    
                    7.6 OpenHarmony与HarmonyOS兼容性
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>

</nav>
     
  </div>

  <!-- Content nav -->
  <div class="book-anchor">
    <div class="book-anchor-title"></div>

    <div class="book-anchor-body"></div>
  </div>

  <div class="book-body">
    
    <div class="body-inner">
       

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >6.2 开发uts插件</a>
    </h1>
</div>



      <div class="page-wrapper" tabindex="-1" role="main">
        <div class="page-inner">
          
<div class="search-plus" id="book-search-results">
    <div class="search-noresults">
    
          <section class="normal markdown-section">
             
                <div id="vip-container">
                    <p>&#x53C2;&#x8003;&#xFF1A;<a href="https://doc.dcloud.net.cn/uni-app-x/plugin/uts-plugin.html" target="_blank">https://doc.dcloud.net.cn/uni-app-x/plugin/uts-plugin.html</a></p>
<h2 id="uni-appx&#x5F00;&#x53D1;&#x9E3F;&#x8499;&#x63D2;&#x4EF6;&#x793A;&#x4F8B;"><a name="uni-appx&#x5F00;&#x53D1;&#x9E3F;&#x8499;&#x63D2;&#x4EF6;&#x793A;&#x4F8B;" class="plugin-anchor" href="#uni-appx&#x5F00;&#x53D1;&#x9E3F;&#x8499;&#x63D2;&#x4EF6;&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>uni-appx&#x5F00;&#x53D1;&#x9E3F;&#x8499;&#x63D2;&#x4EF6;&#x793A;&#x4F8B;</h2>
<h3 id="&#x65B0;&#x5EFA;&#x6B65;&#x9AA4;&#x62C6;&#x89E3;"><a name="&#x65B0;&#x5EFA;&#x6B65;&#x9AA4;&#x62C6;&#x89E3;" class="plugin-anchor" href="#&#x65B0;&#x5EFA;&#x6B65;&#x9AA4;&#x62C6;&#x89E3;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x65B0;&#x5EFA;&#x6B65;&#x9AA4;&#x62C6;&#x89E3;</h3>
<p>&#x53F3;&#x952E;&#x70B9;&#x51FB;<code>uni_modules</code>&#x76EE;&#x5F55; -&gt; &#x65B0;&#x5EFA;&#x63D2;&#x4EF6;</p>
<h2 id="&#x521B;&#x5EFA;&#x63D2;&#x4EF6;"><a name="&#x521B;&#x5EFA;&#x63D2;&#x4EF6;" class="plugin-anchor" href="#&#x521B;&#x5EFA;&#x63D2;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x521B;&#x5EFA;&#x63D2;&#x4EF6;</h2>
<p>&#x5728;HBuilder X &#x4E2D;&#x9009;&#x4E2D;&#x4F60;&#x7684;&#x9879;&#x76EE;&#x4E0B;<code>uni_modules</code>&#x76EE;&#x5F55;&#xFF0C;&#x53F3;&#x952E;&#x9009;&#x62E9;&#x65B0;&#x5EFA;uni_modules&#x63D2;&#x4EF6;, &#x4F8B;&#x5982; <code>uts-api</code></p>
<p>&#x9009;&#x62E9;&#x7C7B;&#x578B; <strong>uts&#x63D2;&#x4EF6;</strong></p>
<p><strong>&#x4E3A;&#x4E86;&#x907F;&#x514D;&#x548C;&#x63D2;&#x4EF6;&#x5E02;&#x573A;&#x7684;&#x5176;&#x4ED6;&#x63D2;&#x4EF6;&#x51B2;&#x7A81;&#xFF0C;&#x5EFA;&#x8BAE;&#x8D77;&#x4E00;&#x4E2A;&#x81EA;&#x5DF1;&#x7684;&#x63D2;&#x4EF6;&#x524D;&#x7F00;&#x540D;&#x79F0;&#x3002;</strong></p>
<p>uts&#x63D2;&#x4EF6;&#x76EE;&#x5F55;&#x7ED3;&#x6784;</p>
<h3 id="packagejson"><a name="packagejson" class="plugin-anchor" href="#packagejson"><i class="fa fa-link" aria-hidden="true"></i></a>package.json</h3>
<p>package.json &#x4E3A; uni_modules &#x63D2;&#x4EF6;&#x914D;&#x7F6E;&#x6E05;&#x5355;&#x6587;&#x4EF6;&#xFF0C;&#x8D1F;&#x8D23;&#x63CF;&#x8FF0;&#x63D2;&#x4EF6;&#x7684;&#x57FA;&#x672C;&#x914D;&#x7F6E;&#x3002;</p>
<pre><code>

</code></pre><p>&#x4E0A;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x7684;&#x6E05;&#x5355;&#x6587;&#x4EF6;&#x793A;&#x4F8B;,&#x5173;&#x4E8E; package.json &#x66F4;&#x591A;&#x63CF;&#x8FF0;<a href="https://uniapp.dcloud.net.cn/plugin/uni_modules.html#package-json" target="_blank">&#x8BE6;&#x89C1;</a></p>
<h3 id="&#x63D2;&#x4EF6;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;"><a name="&#x63D2;&#x4EF6;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;" class="plugin-anchor" href="#&#x63D2;&#x4EF6;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x63D2;&#x4EF6;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;</h3>
<p>&#x6839;&#x76EE;&#x5F55; index.uts &#x6587;&#x4EF6;&#x662F;&#x7A0B;&#x5E8F;&#x4E3B;&#x5165;&#x53E3;&#x3002;&#x5982;&#x679C;&#x63D2;&#x4EF6;&#x6839;&#x76EE;&#x5F55;&#x4E0B;&#x6CA1;&#x6709; index.uts&#xFF0C;&#x5219;&#x4F1A;&#x5728;&#x7F16;&#x8BD1;&#x5230;&#x4E0D;&#x540C;&#x5E73;&#x53F0;&#x65F6;&#xFF0C;&#x5BFB;&#x627E;&#x5206;&#x5E73;&#x53F0;&#x7684;&#x76EE;&#x5F55;&#x4E0B;&#x7684; index.uts &#x6587;&#x4EF6;&#x3002;</p>
<p>&#x6BD4;&#x5982;&#x7F16;&#x8BD1;&#x5230; app-harmony &#x5E73;&#x53F0;&#x65F6;&#xFF0C;&#x5982;&#x679C; uts &#x63D2;&#x4EF6;&#x6839;&#x76EE;&#x5F55;&#x6CA1;&#x6709; index.uts&#xFF0C;&#x4F1A;&#x5BFB;&#x627E; utssdk/app-harmony/index.uts&#x3002;&#x5982;&#x679C;&#x4E5F;&#x6CA1;&#x6709;&#x627E;&#x5230;&#xFF0C;&#x4F1A;&#x62A5;&#x9519;&#x3002;</p>
<p>&#x5F53;&#x540C;&#x65F6;&#x5B58;&#x5728;&#x5206;&#x5E73;&#x53F0;&#x76EE;&#x5F55;&#x7684; index.uts &#x548C;&#x6839;&#x76EE;&#x5F55; index.uts &#x65F6;&#xFF0C;&#x4F1A;&#x4F18;&#x5148;&#x83B7;&#x53D6;&#x5177;&#x4F53;&#x7684;&#x5206;&#x5E73;&#x53F0;&#x76EE;&#x5F55;&#x3002;</p>
<p>&#x5F00;&#x53D1;&#x8005;&#x6709;&#x591A;&#x79CD;&#x7EC4;&#x7EC7;&#x81EA;&#x5DF1;&#x4EE3;&#x7801;&#x7684;&#x65B9;&#x5F0F;&#xFF1A;</p>
<ol>
<li>&#x5728;&#x63D2;&#x4EF6;&#x6839;&#x76EE;&#x5F55;&#x7684; index.uts &#x4E2D;&#x5199;&#x6761;&#x4EF6;&#x7F16;&#x8BD1;&#x4EE3;&#x7801;&#x3002;&#x7B80;&#x5355;&#x7684;&#x4E1A;&#x52A1;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x641E;&#x5B9A;</li>
<li>&#x5728;&#x63D2;&#x4EF6;&#x6839;&#x76EE;&#x5F55; index.uts &#x4E2D;&#x5199;&#x6761;&#x4EF6;&#x7F16;&#x8BD1;&#xFF0C;import &#x5206;&#x5E73;&#x53F0;&#x7684;&#x6587;&#x4EF6;</li>
<li>&#x4E0D;&#x5199;&#x6839;&#x76EE;&#x5F55;&#x7684; index.uts&#xFF0C;&#x76F4;&#x63A5;&#x5728;&#x5206;&#x5E73;&#x53F0;&#x76EE;&#x5F55;&#x5199; index.uts&#x3002;&#x4E0D;&#x8DE8;&#x7AEF;&#x65F6;&#xFF0C;&#x6BD4;&#x5982;&#x53EA;&#x505A;&#x4E00;&#x4E2A; Android &#x63D2;&#x4EF6;&#xFF0C;&#x8FD9;&#x6837;&#x5199;&#x6BD4;&#x8F83;&#x7B80;&#x5355;</li>
</ol>
<p>&#x63D2;&#x4EF6;&#x5BF9;&#x5916;&#x66B4;&#x9732;&#x80FD;&#x529B;&#x7684;&#x603B;&#x5165;&#x53E3;&#x5728; <code>interface.uts</code> &#xFF0C;&#x4ED6;&#x4E0E; <code>index.uts</code>&#x7684;&#x5173;&#x7CFB;&#x662F;&#x58F0;&#x660E;&#x548C;&#x5B9E;&#x73B0;&#x7684;&#x5173;&#x7CFB;</p>
<h2 id="app&#x539F;&#x751F;&#x914D;&#x7F6E;"><a name="app&#x539F;&#x751F;&#x914D;&#x7F6E;" class="plugin-anchor" href="#app&#x539F;&#x751F;&#x914D;&#x7F6E;"><i class="fa fa-link" aria-hidden="true"></i></a>App&#x539F;&#x751F;&#x914D;&#x7F6E;</h2>
<h3 id="&#x9E3F;&#x8499;&#x539F;&#x751F;&#x914D;&#x7F6E;"><a name="&#x9E3F;&#x8499;&#x539F;&#x751F;&#x914D;&#x7F6E;" class="plugin-anchor" href="#&#x9E3F;&#x8499;&#x539F;&#x751F;&#x914D;&#x7F6E;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9E3F;&#x8499;&#x539F;&#x751F;&#x914D;&#x7F6E;</h3>
<p>app-harmony&#x6587;&#x4EF6;&#x5939;&#x5B58;&#x653E;uts&#x63D2;&#x4EF6;&#x7F16;&#x8BD1;&#x5230;&#x9E3F;&#x8499;&#x65F6;&#x7684;&#x4EE3;&#x7801;&#x903B;&#x8F91;&#xFF0C;&#x76EE;&#x524D;&#x4EC5;&#x652F;&#x6301;uts&#x6587;&#x4EF6;&#x3002;</p>
<table>
<thead>
<tr>
<th style="text-align:left">&#x76EE;&#x5F55;&#x540D;/&#x6587;&#x4EF6;&#x540D;</th>
<th style="text-align:left">&#x7528;&#x9014;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">index.uts</td>
<td style="text-align:left">&#x4E3B;&#x5165;&#x53E3;&#xFF0C;interface.uts&#x58F0;&#x660E;&#x7684;&#x80FD;&#x529B;&#x5728;harmony&#x5E73;&#x53F0;&#x4E0B;&#x7684;&#x5B9E;&#x73B0;</td>
</tr>
</tbody>
</table>
<h3 id="hbuilderx&#x9879;&#x76EE;&#x4E2D;uts&#x63D2;&#x4EF6;&#x76EE;&#x5F55;&#x7ED3;&#x6784;"><a name="hbuilderx&#x9879;&#x76EE;&#x4E2D;uts&#x63D2;&#x4EF6;&#x76EE;&#x5F55;&#x7ED3;&#x6784;" class="plugin-anchor" href="#hbuilderx&#x9879;&#x76EE;&#x4E2D;uts&#x63D2;&#x4EF6;&#x76EE;&#x5F55;&#x7ED3;&#x6784;"><i class="fa fa-link" aria-hidden="true"></i></a>HBuilderX&#x9879;&#x76EE;&#x4E2D;uts&#x63D2;&#x4EF6;&#x76EE;&#x5F55;&#x7ED3;&#x6784;</h3>
<p>&#x5728; uni-app / uni-app x &#x7684;&#x9879;&#x76EE;&#x5DE5;&#x7A0B;&#x4E0B;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;&#x72EC;&#x7ACB;&#x7684;&#x76EE;&#x5F55; <code>utssdk</code>&#xFF0C;&#x6765;&#x5B58;&#x653E; uts &#x63D2;&#x4EF6;&#x3002;</p>
<p>&#x5F53;&#x7136;&#x5B98;&#x65B9;&#x66F4;&#x63A8;&#x8350;&#x4F7F;&#x7528; <a href="https://uniapp.dcloud.net.cn/plugin/uni_modules.html" target="_blank">uni_modules</a> &#x65B9;&#x5F0F;&#xFF0C;&#x8FD9;&#x662F;&#x66F4;&#x597D;&#x7684;&#x5305;&#x7BA1;&#x7406;&#x65B9;&#x6848;&#x3002;</p>
<p>&#x9996;&#x5148;&#x786E;&#x4FDD;&#x9879;&#x76EE;&#x6839;&#x76EE;&#x5F55;&#x5B58;&#x5728; uni_modules &#x6587;&#x4EF6;&#x5939;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x9700;&#x8981;&#x624B;&#x52A8;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#xFF0C;&#x76EE;&#x524D;HB&#x4E5F;&#x6709;&#x521B;&#x5EFA;uni_modules &#x7684;&#x6307;&#x5F15;&#xFF0C;&#x8FD8;&#x662F;&#x6BD4;&#x8F83;&#x65B9;&#x4FBF;&#x7684;&#x3002;</p>
<h3 id="&#x7F16;&#x5199;interfaceuts"><a name="&#x7F16;&#x5199;interfaceuts" class="plugin-anchor" href="#&#x7F16;&#x5199;interfaceuts"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7F16;&#x5199;interface.uts</h3>
<p>&#x63D2;&#x4EF6; <code>uts-api</code> &#x521B;&#x5EFA;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x786E;&#x5B9A;&#x63D2;&#x4EF6;&#x5BF9;&#x5916;&#x66B4;&#x9732;&#x7684; API&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x591A;&#x7AEF;&#x7EDF;&#x4E00;&#x89C4;&#x8303;&#x7684;&#x5B9A;&#x4E49;&#x5BF9;&#x5916;&#x66B4;&#x9732;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x83B7;&#x5F97;&#x66F4;&#x597D;&#x7684;&#x8BED;&#x6CD5;&#x63D0;&#x793A;&#x548C;&#x591A;&#x7AEF;&#x4E00;&#x81F4;&#x6027;&#x7EA6;&#x675F;&#xFF0C;&#x6807;&#x51C6;&#x505A;&#x6CD5;&#x662F;&#x5728; <code>interface.uts</code> &#x6587;&#x4EF6;&#x4E2D;&#x7EDF;&#x4E00;&#x5B9A;&#x4E49;&#x63D2;&#x4EF6;&#x8981;&#x66B4;&#x9732;&#x7684; API &#x7C7B;&#x578B;&#x3001; API &#x7684;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#x3001;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x3001;&#x9519;&#x8BEF;&#x7801;&#x7C7B;&#x578B;&#x3001;&#x9519;&#x8BEF;&#x63A5;&#x53E3;&#x7B49;&#x4FE1;&#x606F;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x5404;&#x7AEF;&#x7684; <code>index.uts</code> &#x4E2D;&#x505A;&#x5177;&#x4F53;&#x7684;&#x4E1A;&#x52A1;&#x5B9E;&#x73B0;&#x3002;</p>
<p>&#x6253;&#x5F00; <code>interface.uts</code> &#x6587;&#x4EF6;&#xFF0C;&#x952E;&#x5165;&#x4E0B;&#x9762;&#x7684;&#x6E90;&#x7801;, &#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x8BF4;&#x660E;&#xFF0C;&#x6E90;&#x7801;&#x7684;&#x6BCF;&#x4E2A;&#x90E8;&#x5206;&#x7684;&#x4F5C;&#x7528;&#x90FD;&#x7528;&#x6CE8;&#x91CA;&#x6765;&#x8BF4;&#x660E;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x5B9A;&#x4E49; API&#x7684;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#xFF0C;&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x7684;&#x53C2;&#x6570;&#x65E0;&#x9700;&#x5B9A;&#x4E49;&#xFF0C;&#x590D;&#x6742;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x81EA;&#x5B9A;&#x4E49;type</span>
<span class="hljs-comment">/**
 * myApi &#x5F02;&#x6B65;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x5728;type&#x91CC;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x9700;&#x8981;&#x7684;&#x53C2;&#x6570;&#x4EE5;&#x53CA;api&#x6210;&#x529F;&#x3001;&#x5931;&#x8D25;&#x7684;&#x76F8;&#x5173;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;
 */</span>
<span class="hljs-keyword">export</span> type MyApiOptions = {
  paramA : boolean
  success ?: (res : MyApiResult) =&gt; <span class="hljs-keyword">void</span>
  fail ?: (res : MyApiFail) =&gt; <span class="hljs-keyword">void</span>
  complete ?: (res : any) =&gt; <span class="hljs-keyword">void</span>
}

<span class="hljs-comment">// &#x5B9A;&#x4E49; API &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;, &#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x65E0;&#x9700;&#x7279;&#x6B8A;&#x5B9A;&#x4E49;&#xFF0C;&#x590D;&#x6742;&#x7C7B;&#x578B;&#x7684;&#x53C2;&#x6570;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x81EA;&#x5B9A;&#x4E49;type</span>
<span class="hljs-comment">/**
 * &#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;
 * &#x53EF;&#x4EE5;&#x662F;void, &#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x81EA;&#x5B9A;&#x4E49;type, &#x6216;&#x8005;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x3002;
 * [&#x53EF;&#x9009;&#x5B9E;&#x73B0;]
 */</span>
<span class="hljs-keyword">export</span> type MyApiResult = {
  fieldA : number,
  fieldB : boolean,
  fieldC : string
}

<span class="hljs-comment">// &#x5B9A;&#x4E49; API &#x5BF9;&#x5916;&#x66B4;&#x9732;&#x7684;&#x9519;&#x8BEF;&#x7801;&#xFF0C;&#x4E3A;&#x4E86;&#x66F4;&#x597D;&#x8BED;&#x6CD5;&#x63D0;&#x793A;&#x548C;&#x6821;&#x9A8C;&#x6548;&#x679C;&#xFF0C;&#x5EFA;&#x8BAE;&#x5C06;&#x9519;&#x8BEF;&#x7801;&#x7528;type &#x5B9A;&#x4E49;&#x6210;&#x8054;&#x5408;&#x7C7B;&#x578B;&#x3002;&#x5B9A;&#x4E49;&#x540E;&#xFF0C;&#x4F7F;&#x7528;&#x672A;&#x6307;&#x5B9A;&#x7684;&#x9519;&#x8BEF;&#x7801;&#x5C06;&#x4F1A;&#x88AB;&#x8B66;&#x544A;&#x63D0;&#x793A;&#x3002;</span>
<span class="hljs-comment">// &#x5EFA;&#x8BAE;&#x5B9A;&#x4E49;&#x7684;&#x9519;&#x8BEF;&#x7801;&#x9075;&#x5FAA;uni&#x9519;&#x8BEF;&#x89C4;&#x8303; [&#x8BE6;&#x89C1;](https://uniapp.dcloud.net.cn/tutorial/err-spec.html#unierror)&#x3002;</span>
<span class="hljs-comment">/**
 * &#x9519;&#x8BEF;&#x7801;
 * &#x6839;&#x636E;uni&#x9519;&#x8BEF;&#x7801;&#x89C4;&#x8303;&#x8981;&#x6C42;&#xFF0C;&#x5EFA;&#x8BAE;&#x9519;&#x8BEF;&#x7801;&#x4EE5;90&#x5F00;&#x5934;&#xFF0C;&#x4EE5;&#x4E0B;&#x662F;&#x9519;&#x8BEF;&#x7801;&#x793A;&#x4F8B;&#xFF1A;
 * - 9010001 &#x9519;&#x8BEF;&#x4FE1;&#x606F;1
 * - 9010002 &#x9519;&#x8BEF;&#x4FE1;&#x606F;2
 */</span>
<span class="hljs-keyword">export</span> type MyApiErrorCode = <span class="hljs-number">9010001</span> | <span class="hljs-number">9010002</span>;


<span class="hljs-comment">// &#x5B9A;&#x4E49; API &#x7684;&#x9519;&#x8BEF;&#x56DE;&#x8C03;&#x53C2;&#x6570;&#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x91CC;&#x5B9A;&#x4E49;&#x6210; interface &#x5E76;&#x7EE7;&#x627F; IUniError &#x662F;&#x4E3A;&#x4E86;&#x9075;&#x5FAA;&#x7EDF;&#x4E00;&#x7684; Uni&#x9519;&#x8BEF;&#x7801;&#x89C4;&#x8303;&#x3002;</span>
<span class="hljs-comment">// &#x8FD9;&#x91CC;&#x5F00;&#x53D1;&#x8005;&#x53EA;&#x9700;&#x8981;&#x6307;&#x5B9A; errCode &#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4EE5;&#x4FBF;&#x83B7;&#x5F97;&#x66F4;&#x597D;&#x7684;&#x8BED;&#x6CD5;&#x63D0;&#x548C;&#x6821;&#x9A8C;&#x6548;&#x679C;&#x3002;</span>
<span class="hljs-comment">/**
 * myApi &#x7684;&#x9519;&#x8BEF;&#x56DE;&#x8C03;&#x53C2;&#x6570;
 */</span>
<span class="hljs-keyword">export</span> interface MyApiFail extends IUniError {
  errCode : MyApiErrorCode
};

<span class="hljs-comment">// &#x5B9A;&#x4E49;&#x5BF9;&#x5916;&#x66B4;&#x9732;&#x7684; API &#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x91CC;&#x662F;&#x4E2A;&#x5F02;&#x6B65;&#x51FD;&#x6570;</span>
<span class="hljs-comment">/* &#x5F02;&#x6B65;&#x51FD;&#x6570;&#x5B9A;&#x4E49; */</span>
<span class="hljs-keyword">export</span> type MyApi = (options : MyApiOptions) =&gt; <span class="hljs-keyword">void</span>


<span class="hljs-comment">// &#x5B9A;&#x4E49;&#x5BF9;&#x5916;&#x66B4;&#x9732;&#x7684; API &#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x91CC;&#x662F;&#x4E2A;&#x540C;&#x6B65;&#x51FD;&#x6570;</span>
<span class="hljs-comment">/* &#x540C;&#x6B65;&#x51FD;&#x6570;&#x5B9A;&#x4E49; */</span>
<span class="hljs-keyword">export</span> type MyApiSync = (paramA : boolean) =&gt; MyApiResult
</code></pre>
<blockquote>
<p>&#x7279;&#x522B;&#x6CE8;&#x610F; <code>interface.uts</code> &#x662F;&#x5B98;&#x65B9;&#x63A8;&#x8350;&#x7684;&#x591A;&#x7AEF;&#x4E00;&#x81F4;&#x6027;&#x7684;&#x6700;&#x4F73;&#x5B9E;&#x8DF5;&#xFF0C;&#x4E0D;&#x505A;&#x5F3A;&#x5236;&#x8981;&#x6C42;&#xFF0C;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x81EA;&#x5DF1;&#x7684;&#x5B9E;&#x9645;&#x60C5;&#x51B5;&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x5B9E;&#x73B0;&#x3002;&#x6BD4;&#x5982;&#x67D0;&#x4E2A;&#x63D2;&#x4EF6;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5E73;&#x53F0;&#xFF0C;&#x4E0D;&#x5199;interface&#x4E5F;&#x53EF;&#x4EE5;&#x3002; <code>interface.uts</code> &#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x5E76; <code>export</code> &#x7684; <code>interface</code> &#x63A5;&#x53E3;&#x4F8B;&#x5982; <code>MyApiFail</code> &#x53EA;&#x80FD;&#x5728;&#x63D2;&#x4EF6;&#x5185;&#x90E8;&#x7684; <code>uts</code> &#x6587;&#x4EF6;&#x4EE3;&#x7801;&#x4E2D;&#x4F7F;&#x7528;&#xFF0C;&#x4E0D;&#x80FD;&#x5728; <code>.uvue</code> &#x6587;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528;&#x63D2;&#x4EF6;&#x65F6;&#x5BFC;&#x5165;&#x4F7F;&#x7528;&#x3002;</p>
</blockquote>
<p>&#x81F3;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x5B8C;&#x6210;&#x4E86; <code>interface</code> &#x7684;&#x5B9A;&#x4E49;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x9075;&#x5FAA;&#x89C4;&#x8303;&#xFF0C;&#x5B9A;&#x4E49;&#x4E86;&#x9519;&#x8BEF;&#x7801;&#x7684;&#x7C7B;&#x578B;&#x548C;&#x9519;&#x8BEF;&#x7801;&#x7684; <code>interface</code> &#x5982; <code>MyApiFail</code>, &#x90A3;&#x4E48;&#x4F60;&#x8FD8;&#x9700;&#x8981;&#x5728; <code>unierror.uts</code> &#x6587;&#x4EF6;&#x4E2D;&#x5BF9; <code>MyApiFail</code> &#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x505A;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x3002;</p>
<h3 id="&#x7F16;&#x5199;unierroruts"><a name="&#x7F16;&#x5199;unierroruts" class="plugin-anchor" href="#&#x7F16;&#x5199;unierroruts"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7F16;&#x5199;unierror.uts</h3>
<p>&#x4E3A;&#x4E86;&#x83B7;&#x5F97;&#x66F4;&#x597D;&#x8BED;&#x6CD5;&#x63D0;&#x793A;&#x548C;&#x6821;&#x9A8C;&#x6548;&#x679C;&#xFF0C;&#x6211;&#x4EEC;&#x5728; <code>interface.uts</code> &#x6587;&#x4EF6;&#x4E2D;&#x5DF2;&#x7ECF;&#x5B9A;&#x4E49;&#x4E86;&#x9519;&#x8BEF;&#x7684;&#x7C7B;&#x578B;&#x548C;&#x9519;&#x8BEF;&#x7684;&#x63A5;&#x53E3;&#x3002;&#x4F46;&#x662F;&#x9519;&#x8BEF;&#x7801;&#x5BF9;&#x5E94;&#x7684;&#x5177;&#x4F53;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#xFF0C;&#x4EE5;&#x53CA;&#x9519;&#x8BEF;&#x5BF9;&#x8C61;&#x7684;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#xFF0C;&#x90FD;&#x8FD8;&#x6CA1;&#x6709;&#x5B8C;&#x6210;&#x3002; <code>unierror.uts</code> &#x6587;&#x4EF6;&#x5C31;&#x662F;&#x4E13;&#x95E8;&#x7528;&#x6765;&#x5B9E;&#x73B0;&#x8FD9;&#x4E9B;&#x7684;&#x3002;</p>
<p>&#x6253;&#x5F00; <code>unierror.uts</code> &#x6587;&#x4EF6;, &#x952E;&#x5165;&#x4E0B;&#x9762;&#x7684;&#x6E90;&#x7801;&#x3002;&#x540C;&#x6837;&#x4E3A;&#x4E86;&#x8BF4;&#x660E;&#xFF0C;&#x6E90;&#x7801;&#x7684;&#x6BCF;&#x4E2A;&#x90E8;&#x5206;&#x7684;&#x4F5C;&#x7528;&#x90FD;&#x7528;&#x6CE8;&#x91CA;&#x6765;&#x8BF4;&#x660E;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-comment">// &#x9996;&#x5148;&#x5BFC;&#x5165;&#x5728; interface.uts &#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x9519;&#x8BEF;&#x7801;&#x7C7B;&#x578B;&#xFF0C;&#x548C;&#x9519;&#x8BEF;&#x7684;&#x7C7B;&#x578B;</span>
<span class="hljs-keyword">import</span> { MyApiErrorCode, MyApiFail } from <span class="hljs-string">&quot;./interface.uts&quot;</span>

<span class="hljs-comment">/**
 * &#x5B9A;&#x4E49;&#x9519;&#x8BEF;&#x4E3B;&#x9898;&#xFF0C;&#x9519;&#x8BEF;&#x4E3B;&#x9898;&#x662F;Uni&#x9519;&#x8BEF;&#x7801;&#x7684;&#x4E00;&#x4E2A;&#x6807;&#x51C6;&#x5B57;&#x6BB5;&#x3002;
 * &#x6CE8;&#x610F;&#xFF1A;&#x9519;&#x8BEF;&#x4E3B;&#x9898;&#x4E00;&#x822C;&#x4E3A;&#x63D2;&#x4EF6;&#x540D;&#x79F0;&#xFF0C;&#x6BCF;&#x4E2A;&#x7EC4;&#x4EF6;&#x4E0D;&#x540C;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x65F6;&#x8BF7;&#x66F4;&#x6539;&#x3002;
 * [&#x53EF;&#x9009;&#x5B9E;&#x73B0;]
 */</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> UniErrorSubject = <span class="hljs-string">&apos;uts-api&apos;</span>;


<span class="hljs-comment">/**
 * &#x9519;&#x8BEF;&#x4FE1;&#x606F;&#xFF0C;&#x5B9A;&#x4E49;&#x548C;&#x9519;&#x8BEF;&#x7801;&#x5BF9;&#x5E94;&#x7684;&#x8BED;&#x4E49;&#x5316;&#x7684;&#x63D0;&#x793A;&#x4FE1;&#x606F;&#xFF0C;&#x4E3A;&#x4E86;&#x66F4;&#x597D;&#x7684;&#x83B7;&#x53D6;&#xFF0C;&#x5EFA;&#x8BAE;&#x5B9A;&#x4E49;&#x6210;Map&#x7C7B;&#x578B;&#x3002;
 * @UniError
 * [&#x53EF;&#x9009;&#x5B9E;&#x73B0;]
 */</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> UTSApiUniErrors : Map&lt;MyApiErrorCode, <span class="hljs-built_in">string</span>&gt; = <span class="hljs-keyword">new</span> Map([
  <span class="hljs-comment">/**
   * &#x9519;&#x8BEF;&#x7801;&#x53CA;&#x5BF9;&#x5E94;&#x7684;&#x9519;&#x8BEF;&#x4FE1;&#x606F;
   */</span>
  [<span class="hljs-number">9010001</span>, <span class="hljs-string">&apos;custom error mseeage1&apos;</span>],
  [<span class="hljs-number">9010002</span>, <span class="hljs-string">&apos;custom error mseeage2&apos;</span>],
]);


<span class="hljs-comment">/**
 * &#x9519;&#x8BEF;&#x5BF9;&#x8C61;&#x7684;&#x5177;&#x4F53;&#x4F7F;&#x7528;&#x5B9E;&#x73B0;&#xFF0C;&#x8BE5;&#x5B9E;&#x73B0;&#x4F1A;&#x5728; index.uts&#x4EE3;&#x7801;&#x4E2D;&#x521B;&#x5EFA;&#x4F7F;&#x7528;&#x3002;
 * &#x4F7F;&#x7528;&#x65F6;&#x53EA;&#x9700;&#x8981;&#x4F20;&#x5165;&#x7279;&#x5B9A;&#x7684;&#x9519;&#x8BEF;&#x7801;&#x5373;&#x53EF;&#x5B8C;&#x6210;&#x521B;&#x5EFA;&#x3002;
 */</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> MyApiFailImpl extends UniError <span class="hljs-keyword">implements</span> MyApiFail {
  override errCode: MyApiErrorCode
  <span class="hljs-comment">/**
   * &#x9519;&#x8BEF;&#x5BF9;&#x8C61;&#x6784;&#x9020;&#x51FD;&#x6570;
   */</span>
  <span class="hljs-keyword">constructor</span>(errCode : MyApiErrorCode) {
    <span class="hljs-keyword">super</span>();
    <span class="hljs-keyword">this</span>.errSubject = UniErrorSubject;
    <span class="hljs-keyword">this</span>.errCode = errCode;
    <span class="hljs-keyword">this</span>.errMsg = UTSApiUniErrors.get(errCode) ?? <span class="hljs-string">&quot;&quot;</span>;
  }
}
</code></pre>
<p>&#x81F3;&#x6B64;&#x6211;&#x4EEC;&#x5B8C;&#x6210;&#x4E86;&#x7B26;&#x5408; uni &#x9519;&#x8BEF;&#x89C4;&#x8303;&#x7684;&#x9519;&#x8BEF;&#x7801;&#x7684;&#x5B9A;&#x4E49;&#x548C;&#x5B9E;&#x73B0;&#xFF0C;&#x540E;&#x9762;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x53BB;&#x5B9E;&#x73B0;&#x63D2;&#x4EF6;&#x7684;&#x5177;&#x4F53;&#x903B;&#x8F91;&#x4E86;&#x3002; Uni&#x9519;&#x8BEF;&#x89C4;&#x8303;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;<a href="https://uniapp.dcloud.net.cn/tutorial/err-spec.html#unierror" target="_blank">&#x8BE6;&#x89C1;</a>&#x3002;</p>
<h3 id="&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x5B9A;&#x4E49;&#x548C;&#x4E1A;&#x52A1;&#x903B;&#x8F91;"><a name="&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x5B9A;&#x4E49;&#x548C;&#x4E1A;&#x52A1;&#x903B;&#x8F91;" class="plugin-anchor" href="#&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x5B9A;&#x4E49;&#x548C;&#x4E1A;&#x52A1;&#x903B;&#x8F91;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x5B9A;&#x4E49;&#x548C;&#x4E1A;&#x52A1;&#x903B;&#x8F91;</h3>
<p>&#x5206;&#x522B;&#x5728;&#x63D2;&#x4EF6;&#x7684; <code>app-android</code> &#x3001;<code>app-ios</code> &#x7B49;&#x76EE;&#x5F55;&#x4E0B;&#x6253;&#x5F00; <code>index.uts</code> &#x6587;&#x4EF6;&#xFF0C;&#x952E;&#x5165;&#x4E0B;&#x9762;&#x7684;&#x63D2;&#x4EF6;&#x6E90;&#x7801;:</p>
<p>harmonyOS</p>
<pre><code class="lang-ts">
<span class="hljs-comment">/**
 * &#x5F15;&#x7528;&#x9E3F;&#x8499;&#x7CFB;&#x7EDF;&#x5E93;&#xFF0C;&#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;
 * import deviceInfo from &quot;@ohos.deviceInfo&quot;;
 * [&#x53EF;&#x9009;&#x5B9E;&#x73B0;&#xFF0C;&#x6309;&#x9700;&#x5F15;&#x5165;]
 */</span>

<span class="hljs-comment">/* &#x5F15;&#x5165; interface.uts &#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF; */</span>
<span class="hljs-keyword">import</span> { MyApiOptions, MyApiResult, MyApi, MyApiSync } from <span class="hljs-string">&apos;../interface.uts&apos;</span>;

<span class="hljs-comment">/* &#x5F15;&#x5165; unierror.uts &#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF; */</span>
<span class="hljs-keyword">import</span> { MyApiFailImpl } from <span class="hljs-string">&apos;../unierror&apos;</span>;

<span class="hljs-keyword">export</span> {
  MyApiOptions
}

<span class="hljs-comment">/**
 * &#x5F15;&#x5165;&#x4E09;&#x65B9;&#x5E93;
 * &#x6682;&#x4E0D;&#x652F;&#x6301;&#xFF0C;&#x8BF7;&#x7559;&#x610F;&#x540E;&#x7EED;&#x66F4;&#x65B0;
 */</span>

<span class="hljs-comment">/**
 * &#x5F02;&#x6B65;&#x65B9;&#x6CD5;
 *
 * uni-app&#x9879;&#x76EE;&#x4E2D;&#xFF08;vue/nvue&#xFF09;&#x8C03;&#x7528;&#x793A;&#x4F8B;&#xFF1A;
 * 1&#x3001;&#x5F15;&#x5165;&#x65B9;&#x6CD5;&#x58F0;&#x660E; import { myApi } from &quot;@/uni_modules/uts-api&quot;
 * 2&#x3001;&#x65B9;&#x6CD5;&#x8C03;&#x7528;
 * myApi({
 *   paramA: false,
 *   complete: (res) =&gt; {
 *      console.log(res)
 *   }
 * });
 *
 */</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> myApi : MyApi = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">options : MyApiOptions</span>) </span>{

  <span class="hljs-keyword">if</span> (options.paramA == <span class="hljs-literal">true</span>) {
    <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x6570;&#x636E;</span>
    <span class="hljs-keyword">const</span> res : MyApiResult = {
      fieldA: <span class="hljs-number">85</span>,
      fieldB: <span class="hljs-literal">true</span>,
      fieldC: <span class="hljs-string">&apos;some message&apos;</span>
    };
    options.success?.(res);
    options.complete?.(res);

  } <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x9519;&#x8BEF;</span>
    <span class="hljs-keyword">let</span> failResult = <span class="hljs-keyword">new</span> MyApiFailImpl(<span class="hljs-number">9010001</span>);
    options.fail?.(failResult)
    options.complete?.(failResult)
  }

}

<span class="hljs-comment">/**
 * &#x540C;&#x6B65;&#x65B9;&#x6CD5;
 *
 * uni-app&#x9879;&#x76EE;&#x4E2D;&#xFF08;vue/nvue&#xFF09;&#x8C03;&#x7528;&#x793A;&#x4F8B;&#xFF1A;
 * 1&#x3001;&#x5F15;&#x5165;&#x65B9;&#x6CD5;&#x58F0;&#x660E; import { myApiSync } from &quot;@/uni_modules/uts-api&quot;
 * 2&#x3001;&#x65B9;&#x6CD5;&#x8C03;&#x7528;
 * myApiSync(true);
 *
 */</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> myApiSync : MyApiSync = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">paramA : <span class="hljs-built_in">boolean</span></span>) : <span class="hljs-title">MyApiResult</span> </span>{
  <span class="hljs-comment">// &#x8FD4;&#x56DE;&#x6570;&#x636E;&#xFF0C;&#x6839;&#x636E;&#x63D2;&#x4EF6;&#x529F;&#x80FD;&#x83B7;&#x53D6;&#x5B9E;&#x9645;&#x7684;&#x8FD4;&#x56DE;&#x503C;</span>
  <span class="hljs-keyword">const</span> res : MyApiResult = {
    fieldA: <span class="hljs-number">85</span>,
    fieldB: paramA,
    fieldC: <span class="hljs-string">&apos;some message&apos;</span>
  };
  <span class="hljs-keyword">return</span> res;
}
</code></pre>
<h2 id="&#x4F7F;&#x7528;&#x63D2;&#x4EF6;"><a name="&#x4F7F;&#x7528;&#x63D2;&#x4EF6;" class="plugin-anchor" href="#&#x4F7F;&#x7528;&#x63D2;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4F7F;&#x7528;&#x63D2;&#x4EF6;</h2>
<blockquote>
<p>&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x4E3A;uni-app-x&#x4EE3;&#x7801;</p>
</blockquote>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x6211;&#x4EEC;&#x5B8C;&#x6210;&#x4E86;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; &quot;uts-api&quot; &#x7684;UTS &#x63D2;&#x4EF6;&#xFF0C;&#x5728; <code>uvue</code> &#x6587;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528;&#x8BE5;&#x63D2;&#x4EF6;&#x7684;&#x4EE3;&#x7801;&#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-comment">// &#x5BFC;&#x5165;&#x8981;&#x4F7F;&#x7528;&#x7684;&#x63D2;&#x4EF6;</span>
<span class="hljs-keyword">import</span> { myApi, myApiSync, MyApiOptions } from <span class="hljs-string">&quot;@/uni_modules/uts-api&quot;</span>;

methods: {

    testMyApi() {
        <span class="hljs-comment">// &#x8C03;&#x7528;&#x5F02;&#x6B65;&#x65B9;&#x6CD5;&#x793A;&#x4F8B;</span>
        <span class="hljs-keyword">let</span> options = {
            paramA: <span class="hljs-literal">false</span>,
            complete: (res : <span class="hljs-built_in">any</span>) =&gt; {
            <span class="hljs-built_in">console</span>.log(res)
            }
        } as MyApiOptions;
        myApi(options);
    },

    testMyApiSync() {
        <span class="hljs-comment">// &#x8C03;&#x7528;&#x540C;&#x6B65;&#x65B9;&#x6CD5;&#x793A;&#x4F8B;</span>
        <span class="hljs-built_in">console</span>.log(myApiSync(<span class="hljs-literal">true</span>))
    },
}
</code></pre>
<p>&#x8FD0;&#x884C;&#x548C;&#x7F16;&#x8BD1;uts&#x63D2;&#x4EF6;&#xFF0C;&#x9700;&#x8981;&#x5728;HBuilderX&#x7684;&#x8BBE;&#x7F6E;&#x4E2D;&#x914D;&#x7F6E;Android&#x548C;iOS&#x7684;&#x73AF;&#x5883;&#xFF0C;&#x89C1;&#x5982;&#x4E0B;&#x6587;&#x6863;&#xFF1A;</p>
<ul>
<li><a href="https://uniapp.dcloud.net.cn/tutorial/run/uts-development-android.html" target="_blank">uts&#x63D2;&#x4EF6;Android&#x8FD0;&#x884C;&#x914D;&#x7F6E;</a></li>
<li><a href="https://uniapp.dcloud.net.cn/tutorial/run/uts-development-ios.html" target="_blank">uts&#x63D2;&#x4EF6;iOS&#x8FD0;&#x884C;&#x914D;&#x7F6E;</a></li>
</ul>
<p>&#x5F00;&#x53D1;uts&#x63D2;&#x4EF6;&#xFF0C;&#x8C03;&#x8BD5;&#x3001;&#x6253;&#x65AD;&#x70B9;&#x662F;&#x91CD;&#x8981;&#x5E2E;&#x624B;&#xFF0C;&#x53C2;&#x8003;&#x5982;&#x4E0B;&#x6587;&#x6863;</p>
<ul>
<li><a href="https://uniapp.dcloud.net.cn/tutorial/debug/uni-uts-debug.html" target="_blank">uts&#x63D2;&#x4EF6;Android Debug</a></li>
<li><a href="https://uniapp.dcloud.net.cn/tutorial/debug/uni-uts-debug-ios.html" target="_blank">uts&#x63D2;&#x4EF6;iOS Debug</a></li>
<li><a href="https://uniapp.dcloud.net.cn/tutorial/debug/uni-uts-debug-harmony.html" target="_blank">uts&#x63D2;&#x4EF6;Harmony Debug</a></li>
</ul>
<h2 id="&#x83B7;&#x53D6;&#x7535;&#x91CF;&#x63D2;&#x4EF6;&#x793A;&#x4F8B;"><a name="&#x83B7;&#x53D6;&#x7535;&#x91CF;&#x63D2;&#x4EF6;&#x793A;&#x4F8B;" class="plugin-anchor" href="#&#x83B7;&#x53D6;&#x7535;&#x91CF;&#x63D2;&#x4EF6;&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x83B7;&#x53D6;&#x7535;&#x91CF;&#x63D2;&#x4EF6;&#x793A;&#x4F8B;</h2>
<p>&#x4EE5;&#x83B7;&#x53D6;&#x7535;&#x91CF;&#x4E3A;&#x4F8B;&#xFF0C;&#x4ECB;&#x7ECD;<code>uts</code>&#x63D2;&#x4EF6;&#x5F00;&#x53D1;&#x6B65;&#x9AA4;</p>
<p><strong>&#x9996;&#x5148;&#x5728; <code>uni_modules</code> &#x76EE;&#x5F55;&#x4E0B;&#x65B0;&#x5EFA;&#x540D;&#x4E3A; uts-getbatteryinfo &#x7684; uts &#x63D2;&#x4EF6;</strong></p>
<h3 id="&#x5728;&#x9E3F;&#x8499;&#x5E73;&#x53F0;&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x7F16;&#x8F91;indexuts&#xFF0C;&#x952E;&#x5165;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;&#x3002;"><a name="&#x5728;&#x9E3F;&#x8499;&#x5E73;&#x53F0;&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x7F16;&#x8F91;indexuts&#xFF0C;&#x952E;&#x5165;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;&#x3002;" class="plugin-anchor" href="#&#x5728;&#x9E3F;&#x8499;&#x5E73;&#x53F0;&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x7F16;&#x8F91;indexuts&#xFF0C;&#x952E;&#x5165;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;&#x3002;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5728;&#x9E3F;&#x8499;&#x5E73;&#x53F0;&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x7F16;&#x8F91;index.uts&#xFF0C;&#x952E;&#x5165;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;&#x3002;</h3>
<pre><code class="lang-js"><span class="hljs-comment">// index.uts</span>

<span class="hljs-comment">// &#x5F15;&#x7528;android api</span>
<span class="hljs-keyword">import</span> Context <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;android.content.Context&quot;</span>;
<span class="hljs-keyword">import</span> BatteryManager <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;android.os.BatteryManager&quot;</span>;
<span class="hljs-keyword">import</span> { UTSAndroid } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;io.dcloud.uts&quot;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getBatteryCapacity</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> </span>{
    <span class="hljs-comment">// &#x83B7;&#x53D6;android&#x7CFB;&#x7EDF; application&#x4E0A;&#x4E0B;&#x6587;</span>
    <span class="hljs-keyword">const</span> context = UTSAndroid.getAppContext();
    <span class="hljs-keyword">if</span> (context != <span class="hljs-literal">null</span>) {
        <span class="hljs-keyword">const</span> manager = context.getSystemService(
            Context.BATTERY_SERVICE
        ) <span class="hljs-keyword">as</span> BatteryManager;
        <span class="hljs-keyword">const</span> currentLevel: number = manager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CAPACITY
        );
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;&apos;</span> + currentLevel + <span class="hljs-string">&apos;%&apos;</span>;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;0%&quot;</span>;
}
</code></pre>
<p>&#x81F3;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x5B8C;&#x6210;&#x4E00;&#x4E2A;Android&#x5E73;&#x53F0;&#x4E0A;&#x83B7;&#x53D6;&#x7535;&#x91CF;&#x7684;&#x539F;&#x751F;&#x80FD;&#x529B;&#x5C01;&#x88C5;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;vue&#x9875;&#x9762;&#x4E2D;&#x8FD9;&#x6837;&#x4F7F;&#x7528;&#x5B83;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> { getBatteryCapacity } from <span class="hljs-string">&quot;@/uni_modules/uts-getbatteryinfo&quot;</span>;

<span class="hljs-built_in">console</span>.log(getBatteryCapacity())
</code></pre>
<p>&#x6709;&#x4E9B;&#x573A;&#x666F;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x671F;&#x671B; &#x5C06;&#x83B7;&#x53D6;&#x7535;&#x91CF;&#x7684;&#x80FD;&#x529B;&#x5C01;&#x88C5;&#x4E3A; &#x5F02;&#x6B65;&#x7684;&#x63A5;&#x53E3;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> Context <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;android.content.Context&quot;</span>;
<span class="hljs-keyword">import</span> BatteryManager <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;android.os.BatteryManager&quot;</span>;
<span class="hljs-keyword">import</span> { UTSAndroid } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;io.dcloud.uts&quot;</span>;


type GetBatteryInfoOptions = {
    success?: (res: object) =&gt; <span class="hljs-keyword">void</span>
    fail?: (res: object) =&gt; <span class="hljs-keyword">void</span>
    complete?: (res: object) =&gt; <span class="hljs-keyword">void</span>
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getBatteryInfo</span>(<span class="hljs-params">options: GetBatteryInfoOptions</span>) </span>{
    <span class="hljs-keyword">const</span> context = UTSAndroid.getAppContext();
    <span class="hljs-keyword">if</span> (context != <span class="hljs-literal">null</span>) {
        <span class="hljs-keyword">const</span> manager = context.getSystemService(
            Context.BATTERY_SERVICE
        ) <span class="hljs-keyword">as</span> BatteryManager;
        <span class="hljs-keyword">const</span> level = manager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CAPACITY
        );
        <span class="hljs-keyword">const</span> res = {
            errCode: <span class="hljs-number">0</span>,
            errSubject: <span class="hljs-string">&quot;uts-getbatteryinfo&quot;</span>,
            errMsg: <span class="hljs-string">&quot;getBatteryInfo:ok&quot;</span>,
            level,
            isCharging: manager.isCharging()
        }
        options.success?.(res)
        options.complete?.(res)
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">const</span> res = {
            errCode: <span class="hljs-number">1001</span>,
            errSubject: <span class="hljs-string">&quot;uts-getbatteryinfo&quot;</span>,
            errMsg: <span class="hljs-string">&apos;getBatteryInfo:fail getAppContext is null&apos;</span>
        }
        options.fail?.(res)
        options.complete?.(res)
    }
}
</code></pre>
<p>&#x5BF9;&#x5E94;&#x7684;&#x4F7F;&#x7528;&#x4EE3;&#x7801;&#x9700;&#x8981;&#x8C03;&#x6574;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">import</span> {getBatteryInfo} from <span class="hljs-string">&quot;@/uni_modules/uts-getbatteryinfo&quot;</span>;

getBatteryInfo({
    success(res) {
        uni.showToast({
            title: <span class="hljs-string">&quot;&#x5F53;&#x524D;&#x7535;&#x91CF;&#xFF1A;&quot;</span> + res.level + <span class="hljs-string">&apos;%&apos;</span>,
            icon: <span class="hljs-string">&apos;none&apos;</span>
        });
    }
})
</code></pre>
<h3 id="harmonyos&#x5E73;&#x53F0;"><a name="harmonyos&#x5E73;&#x53F0;" class="plugin-anchor" href="#harmonyos&#x5E73;&#x53F0;"><i class="fa fa-link" aria-hidden="true"></i></a>harmonyOS&#x5E73;&#x53F0;</h3>
<p>&#x5728;utssdk&#x76EE;&#x5F55;&#x4E0B;&#x521B;&#x5EFA;harmonyOS&#x5E73;&#x53F0;&#x76EE;&#x5F55;app-harmony</p>
<p>&#x5728;harmonyOS&#x5E73;&#x53F0;&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x7F16;&#x8F91;index.uts&#xFF0C;&#x952E;&#x5165;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;&#xFF0C;&#x5373;&#x53EF;&#x5B8C;&#x6210;harmonyOS&#x5E73;&#x53F0;&#x83B7;&#x53D6;&#x7535;&#x91CF;&#x80FD;&#x529B;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> batteryInfo <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@ohos.batteryInfo&apos;</span>;
<span class="hljs-keyword">import</span> { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../interface.uts&apos;</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> getBatteryInfoSync : GetBatteryInfoSync = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) : <span class="hljs-title">GetBatteryInfoResult</span> </span>{
  <span class="hljs-keyword">return</span> {
    level: batteryInfo.batterySOC,
    isCharging: batteryInfo.chargingStatus === batteryInfo.BatteryChargeState.ENABLE || batteryInfo.chargingStatus === batteryInfo.BatteryChargeState.FULL,
  };
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> getBatteryInfo : GetBatteryInfo = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">options : GetBatteryInfoOptions</span>) </span>{
  <span class="hljs-keyword">const</span> batteryInfoResult : GetBatteryInfoSuccess = {
    errMsg: <span class="hljs-string">&quot;getBatteryInfo:ok&quot;</span>,
    level: batteryInfo.batterySOC,
    isCharging: batteryInfo.chargingStatus === batteryInfo.BatteryChargeState.ENABLE || batteryInfo.chargingStatus === batteryInfo.BatteryChargeState.FULL,
  }
  <span class="hljs-keyword">try</span> {
    options.success &amp;&amp; options.success(batteryInfoResult)
  } <span class="hljs-keyword">catch</span> (e) {
    <span class="hljs-built_in">console</span>.error(e)
  }
  <span class="hljs-keyword">try</span> {
    options.complete &amp;&amp; options.complete(batteryInfoResult)
  } <span class="hljs-keyword">catch</span> (e) {
    <span class="hljs-built_in">console</span>.error(e)
  }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5C31;&#x662F;&#x6574;&#x4E2A;&#x7684;uni-appx&#x5F00;&#x53D1;&#x9E3F;&#x8499;&#x63D2;&#x4EF6;&#x793A;&#x4F8B;&#xFF0C;&#x5E0C;&#x671B;&#x53EF;&#x4EE5;&#x5E2E;&#x5230;&#x5927;&#x5BB6;&#x3002;</p>
<footer class="page-footer"><span class="copyright">powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2025-06-09 21:26:56
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                </div>
                <script src="https://my.openwrite.cn/js/readmore.js"></script>
                <script>
                    var enablePlugin = false;
                    var allowDomain = "localhost";
                    if(allowDomain){
                        var currentDomain = location.hostname;
                        if ($.isArray(allowDomain)) {
                            $.each(allowDomain, function(index, item) {
                                if (currentDomain == item) {
                                    enablePlugin = true;
                                    return false;
                                }
                            });
                        }else{
                           if (currentDomain == allowDomain) {
                                enablePlugin = true;
                            }
                        }
                    }else{
                        enablePlugin = true;
                    }
                    
                    if(enablePlugin){
                        var isMobile = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i);
                        if (!isMobile) {
                            var btw = new BTWPlugin();
                            btw.init({"allowDomain":"localhost","blogId":"27976-1678928361372-483","name":"坚果派","qrcode":"https://luckly007.oss-cn-beijing.aliyuncs.com/images/image-20230316085818226.png","keyword":"鸿蒙坚果","id":"vip-container"});
                        }
                    }
                </script> 
          </section>
          
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>
 
          <div class="book-footer">
            
            <div class="donate">
              <div></div>
              <button class="donate-btn" onclick="showModal()">赞赏</button>
            </div>
             
            <div class="copyright"></div>
            
          </div>
          
        </div>
      </div>
      
    </div>

     
    <a
      href="1.UTS开发鸿蒙插件.html"
      class="
        navigation navigation-prev
        
      "
      aria-label="Previous page: 6.1 UTS开发鸿蒙插件"
    >
      <i class="fa fa-angle-left"></i>
    </a>
     
    <a
      href="3.新建鸿蒙插件.html"
      class="
        navigation navigation-next
        
      "
      aria-label="Next page: 6.3 新建鸿蒙插件"
    >
      <i class="fa fa-angle-right"></i>
    </a>
      
  </div>
  <script>
    function showModal() {
      document.getElementsByClassName("mask")[0].style.display = "block";
      document.getElementsByClassName("donate-modal")[0].style.display =
        "block";
    }
    // 切换赞赏码
    function showDonateImg(num) {
      let alipay = "";
      let wxpay = ""
      if(num){
        wxpay = "hidden"
      }else{
        alipay = "hidden"
      }
      document.getElementById("wxpay-code").hidden = wxpay;
      document.getElementById("alipay-code").hidden = alipay;
      document.getElementsByName("pay-way")[num].checked = "checked";
    }

    // 关闭赞赏框
    function closeDonateModal() {
      document.getElementsByClassName("mask")[0].style.display = "none";
      document.getElementsByClassName("donate-modal")[0].style.display = "none";
    }

    var gitbook = gitbook || [];
    gitbook.push(function () {
      gitbook.page.hasChanged({"page":{"title":"6.2 开发uts插件","level":"1.6.2","depth":2,"next":{"title":"6.3 新建鸿蒙插件","level":"1.6.3","depth":2,"path":"chapter6/3.新建鸿蒙插件.md","ref":"./chapter6/3.新建鸿蒙插件.md","articles":[]},"previous":{"title":"6.1 UTS开发鸿蒙插件","level":"1.6.1","depth":2,"path":"chapter6/1.UTS开发鸿蒙插件.md","ref":"./chapter6/1.UTS开发鸿蒙插件.md","articles":[]},"dir":"ltr"},"config":{"plugins":["github-buttons@2.1.0","edit-link","splitter","tbfed-pagefooter","expandable-chapters","chapter-fold","back-to-top-button","code","pageview-count","popup","search-plus","-lunr","-search","splitter","-sharing","sharing-plus","theme-lou","mind-maps","emphasize","anchors","changyan","readmore","livereload","ace","livereload"],"root":".","styles":{"website":"styles/website.css"},"pluginsConfig":{"tbfed-pagefooter":{"modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"pluginsConfig":{"expandable-chapters":{}},"emphasize":{},"ace":{},"livereload":{},"splitter":{},"readmore":{"allowDomain":"localhost","blogId":"27976-1678928361372-483","name":"坚果派","qrcode":"https://luckly007.oss-cn-beijing.aliyuncs.com/images/image-20230316085818226.png","keyword":"鸿蒙坚果"},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"popup":{},"code":{"copyButtons":true},"theme-lou":{"hide-elements":[".summary .gitbook-link",".summary .divider"],"color":"#FF4848","book-anchor-title":"本章目录","copyright":{"author":"vx:nut_pie"},"forbidCopy":true,"logo":"assets/avatar.png","book-summary-title":"文章目录","search-placeholder":"输入关键字搜索","copyrightLogo":"assets/copyright.png","favicon":"assets/avatar.png","appleTouchIconPrecomposed152":"assets/avatar.png"},"changyan":{"appid":"cyvHoCPAs","conf":"0440cfc4168ba95d73cfc52dd4d5c60b"},"fontsettings":{"theme":"white","family":"sans","size":2},"mind-maps":{},"highlight":{},"back-to-top-button":{},"pageview-count":{},"github-buttons":{"repo":"ITmxs/gitbook","types":["star","watch","fork"],"size":"small"},"sharing":{"qq":false,"all":["qq","qzone","whatsapp","douban","facebook","google","instapaper","linkedin","messenger","twitter","weibo"],"douban":false,"facebook":false,"weibo":false,"twitter":false,"pocket":false,"google":false,"qzone":false},"edit-link":{"label":"编辑此页","base":"https://github.com/ITmxs/gitbook/edit/master/"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"expandable-chapters":{},"search-plus":{}},"theme":"default","author":"坚果派","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"themeLou":{"nav":[{"target":"_blank","url":"https://www.nutpi.net/","name":"坚果派官网"},{"target":"_blank","url":"https://www.arkui.club/","name":"ArkUI实战"},{"target":"_blank","url":"https://space.bilibili.com/480883651","name":"元服务开发实践"},{"target":"_blank","url":"https://gitcode.com/nutpi","name":"OpenHarmony应用开发"}],"footer":{"donate":{"wechat":"https://luckly007.oss-cn-beijing.aliyuncs.com/img/image-20210922202901895.png","message":"随意打赏，但不要超过一顿早餐钱 💕","wxpay":"https://luckly007.oss-cn-beijing.aliyuncs.com/img/image-20210922202901895.png","alipay":"https://luckly007.oss-cn-beijing.aliyuncs.com/img/image-20210922202843746.png","nickname":"breeze","button":"打赏","wechatText":"微信","alipayText":"支付宝","title":"『赠人玫瑰 🌹 手有余香 』","avatar":"http://xkapp-uat.oss-cn-hangzhou.aliyuncs.com/2e7a3f70-80ab-4b50-93ec-a04dfeef949b/avatar-100.png"},"copyright":true}}},"title":"uniapp/uniapp-x for Harmony教程","language":"zh-hans","gitbook":"*","description":"uniapp/uniapp-x for Harmony电子书教程"},"file":{"path":"chapter6/2.开发uts插件.md","mtime":"2025-06-09T13:26:56.102Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2025-06-30T01:19:14.211Z"},"basePath":"..","book":{"language":""}});
    });
  </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-mind-maps/mindmaps.dist.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-changyan/changyan.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-ace/ace/ace.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-ace/ace.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-lou/lou.js"></script>
        
    

    </body>
</html>

